package com.crecheng.shop.Manage;

import com.crecheng.shop.DAO.UserCartData;
import com.crecheng.shop.util.ConSQL;
import com.crecheng.shop.util.ListUtil;
import org.apache.catalina.User;

import java.util.List;

public final class UserCartManage {
    public static List<UserCartData> getAll(){
        return ConSQL.select(UserCartData.class);
    }

    public static List<UserCartData> get(String other){
        return ConSQL.select(UserCartData.class,other);
    }

    public static UserCartData get(int id){
        return ListUtil.first(get("id="+id));
    }

    public static List<UserCartData> getByUser(int uid){
        return get("uid="+uid);
    }

    public static boolean add(UserCartData data){
        var d=ListUtil.first(
                get("uid="+data.getUid()+" and sid="+data.getSid()+" and gid="+data.getGid()));
        if (d != null) {
            d.setNum(d.getNum()+data.getNum());
            return update(d);
        }
        else
            return ConSQL.insert(UserCartData.class,data)>0;
    }

    public static boolean del(int id){
        return ConSQL.delete(UserCartData.class,"id="+id)>0;
    }

    public static boolean del(UserCartData data){
        return del(data.getId());
    }

    public static int delByUser(int uid){
        return ConSQL.delete(UserCartData.class,"uid="+uid);
    }

    public static boolean update(UserCartData data){
        return ConSQL.update(UserCartData.class,data.key(),data)>0;
    }

    public static boolean changeNum(int id,int addNum){
        var d=get(id);
        if (d != null) {
            int a=d.getNum();
            if(a+addNum<1)
                return false;
            d.setNum(d.getNum()+addNum);
            return update(d);
        }
        return false;
    }



}
